package com.lindroy.androidplayer.ui.knowledge

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import com.lindroy.androidplayer.R
import com.lindroy.androidplayer.base.BaseLazyFragment
import com.lindroy.androidplayer.base.eventViewModel
import com.lindroy.androidplayer.extensions.initColor
import com.lindroy.androidplayer.extensions.loadListData
import com.lindroy.androidplayer.extensions.setLineDivider
import com.lindroy.androidplayer.extensions.yes
import com.lindroy.androidplayer.logic.model.bean.CollectBus
import com.lindroy.androidplayer.ui.adapter.HomeDataAdapter
import com.lindroy.androidplayer.ui.collection.CollectionActivity
import com.lindroy.androidplayer.ui.webview.ContentActivity
import com.lindroy.androidplayer.viewmodel.CollectViewModel
import com.lindroy.androidplayer.viewmodel.KnowledgeViewModel
import com.lindroy.lib_utils.extensions.extraParcelable
import kotlinx.android.synthetic.main.fragment_knowledge_detail.*


/**
 * @author Lin
 * @date 2021/4/29
 * @function 知识体系详情页面
 * @Description
 */
private const val KEY_ID = "id"

class KnowledgeDetailFragment(override val layoutId: Int = R.layout.fragment_knowledge_detail) :
    BaseLazyFragment() {


    private val knowledgeViewModel: KnowledgeViewModel by viewModels()

    private val adapter by lazy { HomeDataAdapter() }

    private val collectViewModel: CollectViewModel by viewModels()

    override fun initArguments(bundle: Bundle) {
        super.initArguments(bundle)
        knowledgeViewModel.cid = bundle.getInt(KEY_ID)
    }

    override fun initView() {
        refreshHeader.initColor()
        rvKnowledgeDetail.setLineDivider()
        rvKnowledgeDetail.adapter = adapter
        addLoadingObserve(collectViewModel)
    }

    override fun lazyLoad() {
        reqKnowledgeArticle(true)
    }

    override fun initListener() {
        refreshLayout.setOnRefreshListener {
            reqKnowledgeArticle(true)
        }
        refreshLayout.setOnLoadMoreListener {
            reqKnowledgeArticle(false, false)
        }
        knowledgeViewModel.knowledgeDetailState.observe(this) {
            loadListData(it, adapter, refreshLayout, stateLayout)
        }
        collectViewModel.collectState.observe(this) {
            it.success.yes {
                eventViewModel.collectEvent.value = CollectBus(it.id, it.collect, it.originId)
            }
        }
        eventViewModel.collectEvent.observeInFragment(this) { collectBus ->
            val index = adapter.data.indexOfFirst {
                it.id == collectBus.id
            }
            if (index >= 0) {
                adapter.data[index].collect = collectBus.collect
                adapter.notifyItemChanged(index)
            }
        }
        adapter.setOnCollectClickListener { collect, id ->
            collectViewModel.collectArticle(id)
        }
        adapter.setOnItemClickListener { _, view, position ->
            val item = adapter.data[position]
            context?.let { ContentActivity.start(it, item.id, item.link, item.title, item.collect) }
        }
    }

    override fun initClick() {
    }

    private fun reqKnowledgeArticle(showLoading: Boolean, isRefresh: Boolean = true) {
        showLoading.yes { stateLayout.showLoading() }
        knowledgeViewModel.reqKnowledgeDetail(isRefresh)
    }

    companion object {
        @JvmStatic
        fun newInstance(cid: Int) =
            KnowledgeDetailFragment().apply {
                arguments = Bundle().apply {
                    putInt(KEY_ID, cid)
                }
            }
    }
}